var agCamera = agcim.camera.camera;
var primitiveHelper=agcim.layer.primitiveHelper;
let scratch = new Cesium.Matrix4();
/**
 * Class 相机更新类
 */
class CameraUpdater {
    /**
     * @description: 重置参数
     * @param {method} ph 
     * @param {Object} roam 
     */
    setEnv(ph, roam) {
        this.parentHandler = ph;
        this.roam = roam;
        this.listenerRoamingBind = this.listenerRoaming.bind(this);
        this.cesium3DTilesets=[];
    } 
    /**
     * @description: 监听并设置当前时间点的相机位置
     * @returns 
     */
    listenerRoaming() {
        let _this = this.parentHandler;
        let roam = this.roam;
        let viewer = CIM.viewer;

        if (!_this.isContinue)
            return;
        if (
            _this.canRestart &&
            viewer.clock.stopTime.secondsOfDay ===
            viewer.clock.currentTime.secondsOfDay
        ) {
            roam.initRoaming("restart");
        }
        if (
            viewer.clock.stopTime.secondsOfDay !==
            viewer.clock.currentTime.secondsOfDay
        ) {
            _this.canRestart = false;
            if (_this.isLock == true) { 
                this.getModelMatrixInTime(
                    roam.entity,
                    viewer.clock.currentTime,
                    scratch,
                    roam.cameraHeight
                );
                viewer.scene.camera.lookAtTransform(
                    scratch,
                    new Cesium.HeadingPitchRange(
                        Cesium.Math.toRadians(90),
                        Cesium.Math.toRadians(-roam.angle),
                        roam.width
                    )
                );
            }
        } else {
            if (document.getElementById("btnRoad")){
                document.getElementById("btnRoad").innerText = "开始漫游";
            }
            _this.isContinue = false;
            _this.canRestart = true;
            agCamera.resetCamera(viewer);
            if(_this.resetPlayIcon){
                _this.resetPlayIcon();

                _this.clearDraw();
            }
            viewer.scene.preUpdate.removeEventListener(this.listenerRoamingBind);
        }
    }
    getModelMatrixInTime(entity, time, result, addHeight) {
        let matrix3Scratch = new Cesium.Matrix3();
        if (!entity) return undefined;
        let position = Cesium.Property.getValueOrUndefined(
          entity.position,
          time,
          new Cesium.Cartesian3()
        );
        if (!Cesium.defined(position)) {
          return undefined;
        }
        let lnglat = Cesium.Cartographic.fromCartesian(position);
        if(addHeight){
            lnglat.height += addHeight;
        }
        position = Cesium.Cartesian3.fromRadians(
          lnglat.longitude,
          lnglat.latitude,
          lnglat.height
        );
        let orientation = Cesium.Property.getValueOrUndefined(
          entity.orientation,
          time,
          new Cesium.Quaternion()
        );
        if (!Cesium.defined(orientation)) {
          result = Cesium.Transforms.eastNorthUpToFixedFrame(
            position,
            undefined,
            result
          );
        } else {
          result = Cesium.Matrix4.fromRotationTranslation(
            Cesium.Matrix3.fromQuaternion(orientation, matrix3Scratch),
            position,
            result
          );
        }
        return result;
      }
    /**
     * @description: 改变视角宽度
     * @param {*} value 
     */
    changeWidth(value){
        if(this.roam){
            this.roam.width = value;
        }
    }
    /**
     * @description: 改变视角角度
     * @param {*} value 
     */
    changeAngle(value){
        if(this.roam){
            this.roam.angle = value;
        }
    }
    /**
     * @description: 场景监听
     * @param {Object} viewer 
     */
    preUpdateListener(viewer) {
        viewer.scene.preUpdate.addEventListener(this.listenerRoamingBind);
    }
}
export default new CameraUpdater();